package com.example.finalapplication;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.ListFragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FirstFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final String TAG = "FirstFragment";

    private Handler handler;
    private ProgressBar bar;
    private GridView gridView;
    private ArrayAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first,container,false);

            gridView = view.findViewById(R.id.list);
            bar = view.findViewById(R.id.progressBar);
            gridView.setEmptyView(bar);
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(this);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if (msg.what==5){

                    Log.i(TAG, "handleMessage: mmm");
                    List<String> list2= (List<String>) msg.obj;
                    adapter = new ArrayAdapter(getActivity(),
                            R.layout.grid_item,
                            list2);
                    gridView.setAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
        //开启子线程
        MyTask myTask = new MyTask();
        myTask.setHandler(handler);
        Thread t = new Thread(myTask);
        t.start();
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: pos=" + position);
            String position1 = String.valueOf(position);
            Intent intent = new Intent(getActivity(), FoodActivity1.class);
            intent.putExtra("location",position1);
            startActivity(intent);
    }


}